﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Microsoft.Practices.Prism.Regions;
using Tww.Paf.Api.Components.NavigationMenu;

namespace Tww.Paf.Api.RegionAdapter
{
    public class NavigationMenuRegionAdapter : RegionAdapterBase<NavigationMenu>
    {
        #region  - Felder (privat) -

        private readonly List<string> uniqueNameCollection = new List<string>();
        private NavigationMenu menuRegionTarget;
        private IRegion region;

        #endregion

        #region  - Konstruktoren -

        public NavigationMenuRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        #endregion

        #region  - Indexer und Eigenschaften (öffentlich) -

        public List<string> UniqueNameCollection
        {
            get { return uniqueNameCollection; }
        }

        #endregion

        #region  - Methoden (privat) -

        protected override void Adapt(IRegion region, NavigationMenu regionTarget)
        {
            menuRegionTarget = regionTarget;
            this.region = region;

            region.ActiveViews.CollectionChanged += ActiveViewsOnCollectionChanged;

            foreach (NavigationMenu expandMenuView in region.ActiveViews)
            {
                regionTarget.Items.Add(expandMenuView);
            }
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }

        private void ActiveViewsOnCollectionChanged(object sender,
            NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {
            switch (notifyCollectionChangedEventArgs.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    List<NavigationMenuItem> addingNavigationMenuItems =
                        notifyCollectionChangedEventArgs.NewItems.OfType<NavigationMenuItem>().ToList();
                    List<NavigationMenu> addingNavigationMenus =
                        notifyCollectionChangedEventArgs.NewItems.OfType<NavigationMenu>().ToList();

                    addingNavigationMenus.ForEach(AddNavigationMenu);
                    addingNavigationMenuItems.ForEach(AddNavigationMenuItem);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    List<NavigationMenuItem> removingNavigationMenuItems =
                        notifyCollectionChangedEventArgs.OldItems.OfType<NavigationMenuItem>().ToList();
                    List<NavigationMenu> removingNavigationMenus =
                        notifyCollectionChangedEventArgs.OldItems.OfType<NavigationMenu>().ToList();

                    removingNavigationMenus.ForEach(RemoveNavigationMenu);
                    removingNavigationMenuItems.ForEach(RemoveNavigationMenuItem);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    break;
                case NotifyCollectionChangedAction.Move:
                    break;
                case NotifyCollectionChangedAction.Reset:
                    var viewCollection = sender as IViewsCollection;

                    if (viewCollection != null)
                    {
                        INavigationMenu adapter = viewCollection.OfType<INavigationMenu>().FirstOrDefault();

                        if (adapter != null)
                        {
                            region.Activate(adapter);
                        }
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void AddNavigationMenu(NavigationMenu expandMenu)
        {
            foreach (NavigationMenuItem expandMenuItem in expandMenu.Items.OfType<NavigationMenuItem>().ToList())
            {
                if (string.IsNullOrWhiteSpace(expandMenuItem.Name) ||
                    UniqueNameCollection.Any(u => u.ToLower().Trim() == expandMenuItem.Name))
                {
                    throw new UpdateRegionsException(
                        string.Format(
                            "NavigationMenuItem must have a unique Name for classification. The NavigationMenuItem with the Name '{0}' already exists.",
                            expandMenuItem.Name));
                }

                expandMenu.Items.Remove(expandMenuItem);
                AddNavigationMenuItem(expandMenuItem);
            }
        }

        private void AddNavigationMenuItem(NavigationMenuItem expandMenuItem)
        {
            menuRegionTarget.Items.Add(expandMenuItem);
            UniqueNameCollection.Add(expandMenuItem.Name);
        }

        private void RemoveNavigationMenu(NavigationMenu expandMenu)
        {
            foreach (NavigationMenuItem expandMenuItem in expandMenu.Items.OfType<NavigationMenuItem>().ToList())
            {
                //expandMenu.Items.Remove(expandMenuItem);
                //this.RemoveNavigationMenuItem(expandMenuItem);
            }
        }

        private void RemoveNavigationMenuItem(NavigationMenuItem expandMenuItem)
        {
            //this.menuRegionTarget.Items.Add(expandMenuItem);
            UniqueNameCollection.Remove(expandMenuItem.Name);
        }

        #endregion
    }
}